package com.eascs.web.p2p.loginAction;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.eascs.p2p.common.entity.AdminResourceEntity;
import com.eascs.p2p.common.entity.AdminUserEntity;
import com.eascs.p2p.common.service.IAdminResourceService;
import com.eascs.p2p.common.vo.AdminResourcesVo;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;

@RestController
@RequestMapping("/act/resource/rest")
public class ResourceRestController extends BaseRestController {
	@Reference
	private IAdminResourceService iAdminResourceService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public BaseRestResult<String> list() {
		ModelResult<List<AdminResourceEntity>> modelResult = iAdminResourceService.selectResourcesList();
		List<AdminResourceEntity> list = modelResult.getModel();
		return BaseRestResult.buildRestResult(JSON.toJSONString(list));
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public BaseRestResult<String> update(AdminResourcesVo form,HttpServletRequest request) {
		AdminResourceEntity sysResources = SysResourcesConvert(form,request);
		ModelResult<List<AdminResourceEntity>> model = iAdminResourceService.selectResourcesListByCode(sysResources.getResourceCode());
		List<AdminResourceEntity> list=model.getModel();
		int count = list.size();
		if (count == 1 && (!list.get(0).getId().equals(sysResources.getId()))) {
			return BaseRestResult.warn("资源Code不能重复!");
		}
		if (count > 1) {
			return BaseRestResult.warn("资源Code不能重复!");
		}
		ModelResult<Boolean> modelResult = iAdminResourceService.updateResources(sysResources);
		Boolean result = modelResult.getModel();
		if (result==true) {
			return BaseRestResult.warn("修改成功!");
		} else {
			return BaseRestResult.warn("修改失败!");
		}
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public BaseRestResult<String> save(AdminResourcesVo form,HttpServletRequest request) {
		AdminResourceEntity sysResources = SysResourcesConvert(form,request);
		ModelResult<String> modelResult = iAdminResourceService.insertResources(sysResources);
		String result = modelResult.getModel();
		if (result != null) {
			return BaseRestResult.buildRestResult(JSON.toJSONString(result));
		} else {
			return BaseRestResult.warn("新增失败!");
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public BaseRestResult<String> delete(AdminResourceEntity sysResources) {
		ModelResult<Integer> modelResult = iAdminResourceService.deleteResources(sysResources);
		Integer result = modelResult.getModel();
		if (result > 0) {
			return BaseRestResult.warn("删除成功!");
		} else {
			return BaseRestResult.warn("删除失败!");
		}
	}

	private AdminResourceEntity SysResourcesConvert(AdminResourcesVo resform,HttpServletRequest request) {
		AdminResourceEntity sysres = new AdminResourceEntity();
		sysres.setId(resform.getId());
		sysres.setResourceName(resform.getResourceName());
		sysres.setResourceCode(resform.getResourceCode());
		sysres.setParentId(resform.getParentId());
		sysres.setResourceType(resform.getResourceType());
		sysres.setResourceDefault(resform.getResourceDefault());
		sysres.setResourceUrl(resform.getResourceUrl());
		sysres.setDescription(resform.getDescription());
		sysres.setSort(resform.getSort());
		sysres.setUpdateTime(new Date());
		sysres.setCreateTime(new Date());
		HttpSession session =request.getSession();
		AdminUserEntity userName=(AdminUserEntity) session.getAttribute("adminUser");
		sysres.setUpdator(userName!=null?userName.getAccount():"0");
		sysres.setCreator(userName!=null?userName.getAccount():"0");
		return sysres;
	}

}
